package com.PopulateDB;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.CMUBiS.util.Address;

public class PopulateUsers implements PopulateFormat{
	static ArrayList<String> firstNames = PopulateUsers.readStrings(FileLocations.firstNamesFile);
	static ArrayList<String> lastNames = PopulateUsers.readStrings(FileLocations.lastNamesFile);
	private static int firstNamesSize = firstNames.size();
	private static int lastNamesSize = lastNames.size();
	
	final static int minAddresses = 1; // Min 1
	final static int maxAddresses = 5;
	final static int addressDifference = maxAddresses - minAddresses;
	final static int minUserRating = -100;
	final static int maxUserRating = 100;
	final static int userRatingDifference = maxAddresses - minAddresses;
	
	public boolean makeThing(DBPopulator db, int usersInserted){
		String username = getUsername(usersInserted);
		
		/*
		 * Make a user
		 */
		int userID = db.insertUser(getFirstName(),
				getLastName(),
				username,
				username,
				getEmail(username),
				getRating(),
				new Date(System.currentTimeMillis() - Math.round((Math.random()*100000000000l))));
		
		/*
		 * Make the user's addresses
		 */
		// Insert the user's addresses they must have one at least
		if(minAddresses > 0){
			Address address = MakeAddress.getAddress(userID, true);
			db.insertAddress(address);
		}
		
		// Make the other addresses
		int length = (int)Math.round(Math.random() * addressDifference);
		
		for(int i=0; i < length; i++){
			Address address = MakeAddress.getAddress(userID, false);
			db.insertAddress(address);
		}
		
		return true;
	}
	
	public boolean remakeTable(DBPopulator db){
		db.dropAddAdresses();
		return db.dropAddUsers();
	}
	
	private static ArrayList<String> readStrings(String file){
		ArrayList<String> temp = new ArrayList<String>();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(MakeAddress.class.getResourceAsStream(file)));
			
			try {
				// Read in the strings
				while(in.ready()){
					String firstWord = in.readLine().split(",")[0];
					temp.add(firstWord);
				}
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return temp;
	}
	
	/*
	 * Make the users first name
	 */
	private static String getFirstName(){
		return firstNames.get(((int)Math.floor(Math.random() * firstNamesSize)));
	}
	
	private static String getLastName(){
		return lastNames.get(((int)Math.floor(Math.random() * lastNamesSize)));
	}
	
	private static String getUsername(int uniqueID){
		return "User" + uniqueID;
	}
	
	private static String getEmail(String username){
		return username + "@ece.cmu.edu";
	}
	
	private static int getRating(){
		return (int)Math.floor(Math.random() * userRatingDifference) + minUserRating;
	}
}
